package com.banciyuan.bcywebview.utils.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

//判断网络环境的一些工具类，自己看
public class NetUtils {

	// wap网络汇总
	public static final String CTWAP = "ctwap";
	public static final String CMWAP = "cmwap";
	public static final String WAP_3G = "3gwap";
	public static final String UNIWAP = "uniwap";

	/**
	 * 获取网络类型
	 *
	 * @param context
	 * @return
	 */
	public static String getNetworkType(Context context) {

		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo activeNetInfo = connectivityManager
					.getActiveNetworkInfo();
			if (activeNetInfo != null) {
				return activeNetInfo.getTypeName();
			} else {
				return "other";
			}
		} catch (Exception e) {
			return "other";
		}
	}

	/**
	 * 检查当前网络是否可用
	 *
	 * @param context
	 * @return
	 */
	public static boolean checkNetwork(Context context) {

		boolean flag = false;

		try {
			ConnectivityManager cwjManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (cwjManager.getActiveNetworkInfo() != null)
				flag = cwjManager.getActiveNetworkInfo().isConnected();// .isAvailable();
		} catch (Exception e) {

		}

		return flag;
	}

	/**
	 * 判断当前网络是否是wifi
	 *
	 * @param context
	 * @return
	 */
	public static boolean isWifi(Context context) {
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo activeNetInfo = connectivityManager
					.getActiveNetworkInfo();
			if (activeNetInfo != null
					&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 判断当前网络是否是wap
	 *
	 * @param context
	 * @return
	 */
	public static boolean isCMWAPMobileNet(Context context) {

		if (null == context) {
			return false;
		}

		if (isWifi(context)) {
			return false;
		} else {
			ConnectivityManager mag = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			if (null != mag) {
				NetworkInfo mobInfo = mag
						.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
				if (null != mobInfo) {
					String extrainfo = mobInfo.getExtraInfo();
					if (null != extrainfo) {
						extrainfo = extrainfo.toLowerCase();
						return extrainfo.contains("wap");
					} else {
						return false;
					}

				} else {
					return false;
				}
			} else {
				return false;
			}
		}

	}
}
